package com.itheima.web;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.ResultInfo;
import com.itheima.domain.Role;
import com.itheima.factory.BeanFactory;
import com.itheima.service.RoleService;
import com.itheima.service.impl.RoleServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@WebServlet("/role")
public class RoleServlet extends BaseServlet{
    //调用service查询所有角色
    //private RoleService roleService = new RoleServiceImpl();
    private RoleService roleService =(RoleService) BeanFactory.getBean("roleService");

    /*
        查询所有
     */
    private void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            //1. 调用service查询所有角色
            List<Role> roles = roleService.findAll();
            //System.out.println(1 / 0);
            //2. 将集合封装到ResultInfo
            ResultInfo resultInfo = new ResultInfo(true, "查询成功", roles);
            //3. 将ResultInfo转成json字符串，回复给浏览器
            writeJson(response, resultInfo);
        } catch (Exception e) {
            //如果遇到问题，会执行catch，就手动封装一个ResultInfo，回复给浏览器
            e.printStackTrace();
            ResultInfo resultInfo = new ResultInfo(false, "服务器忙，稍后再试");
            writeJson(response, resultInfo);
        }
    }



    /*
        添加方法
     */
    private void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            //1.如果浏览器发送过来的是一个json，需要通过流的方式去接收
            //这个流中就是客户端发送来的数据
            InputStream in = request.getInputStream();
            //2. 将流转成对象
            ObjectMapper objectMapper = new ObjectMapper();
            Role role = objectMapper.readValue(in, Role.class);
            //3. 调用service添加
            roleService.save(role);
            //4. 创建ResultInfo封装结果
            ResultInfo resultInfo = new ResultInfo(true, "添加成功");
            //5. 将ResultInfo回复给浏览器
            writeJson(response, resultInfo);
        } catch (Exception e) {
            e.printStackTrace();
            //封装添加失败的结果
            ResultInfo resultInfo = new ResultInfo(false, "添加失败，服务器忙");
            //回复给浏览器
            writeJson(response, resultInfo);
        }
    }

    /*
        分页查询
     */
    private void findByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            //1. 接收请求参数
            String pageNumStr = request.getParameter("pageNum");
            String pageSizeStr = request.getParameter("pageSize");
            //给出默认值
            if (pageNumStr == null || pageNumStr == "") {
                pageNumStr = "1";
            }
            if (pageSizeStr == null || pageSizeStr == "") {
                pageSizeStr = "5";
            }
            //转型
            Integer pageNum = Integer.valueOf(pageNumStr);
            Integer pageSize = Integer.valueOf(pageSizeStr);
            //2. 调用service进行分页查询
            PageInfo<Role> pageInfo = roleService.findByPage(pageNum, pageSize);
            //3. 封装到ResultInfo
            ResultInfo resultInfo = new ResultInfo(true, "查询成功", pageInfo);
            //4. 将ResultInfo回复给页面
            writeJson(response, resultInfo);
        } catch (Exception e) {
            e.printStackTrace();
            ResultInfo resultInfo = new ResultInfo(false, "服务器忙");
            writeJson(response, resultInfo);
        }
    }

    /*
        根据id查询
     */
    private void findById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            //1.接收请求参数
            String idStr = request.getParameter("id");
            //将id转成Integer
            Integer id = Integer.valueOf(idStr);
            //2. 调用service进行查询
            Role role = roleService.findById(id);
            //3. 封装到ResultInfo
            ResultInfo resultInfo = new ResultInfo(true, "查询成功", role);
            //4. 将ResultInfo转成json
            writeJson(response, resultInfo);
        } catch (Exception e) {
            e.printStackTrace();
            ResultInfo resultInfo = new ResultInfo(false, "服务器忙");
            writeJson(response, resultInfo);
        }
    }

    /*
        模板，以后复制使用
     */
    private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            //1. 获取输入流
            InputStream in = request.getInputStream();
            //2. 将流【json】转成Role
            ObjectMapper objectMapper = new ObjectMapper();
            Role role = objectMapper.readValue(in, Role.class);
            //3. 调用service修改
            roleService.update(role);
            //4. 使用ResultInfo封装结果
            ResultInfo resultInfo = new ResultInfo(true, "修改成功");
            //5. 回复给浏览器
            writeJson(response, resultInfo);
        } catch (Exception e) {
            e.printStackTrace();
            ResultInfo resultInfo = new ResultInfo(false, "服务器忙，修改失败");
            writeJson(response, resultInfo);
        }
    }

    /*
        根据id删除
     */
    private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            //获取请求参数id
            String idStr = request.getParameter("id");
            Integer id = Integer.valueOf(idStr);
            //调用service进行删除
            roleService.delete(id);
            //给出回复
            ResultInfo resultInfo = new ResultInfo(true, "删除成功");
            writeJson(response, resultInfo);
        } catch (Exception e) {
            e.printStackTrace();
            ResultInfo resultInfo = new ResultInfo(false, "服务器忙，删除失败");
            writeJson(response, resultInfo);
        }
    }
}
